<?php
class Comment
{		
	
	public function __construct ()
	{
		
		
	}
	
	/**
	*	Esta funcion es responsable de insertar datos en la tabla Comment
	*	@autor: JJR
	*	@date: 01/28/2012
	*	@parameters: 
	*	@return:
	*/
	function addComment($data){
		$sql = "INSERT INTO comment (
		com_code,
		case_code,
		com_comment,
		com_date_create,
		use_code)
		VALUES (
		NULL,
		'".$data->case_code."',
		'".$data->com_comment."',
		'".$data->com_date_create."',
		'".$data->use_code."');"; 
		
		return mysql_query ($sql);
	}	

	
	/**
	*	Esta funcion es la encargada de listar los comentarios que existen en el sitio.
	*	@autor: JJR
	*	@date: 01/28/2012
	*	@parameters: 
	*	@return:
	*/
	function commentList($search='',$order_by='', $inicio = 0,$cantidad = 0){
		$where = '';		
		if($search != '')
		$where = "  WHERE (	           
				    cli_name like '%".$search."%'
				   OR cli_lastname like '%".$search."%'
				   OR case_number like '%".$search."%'
				   OR use_name like '%".$search."%'
				   OR use_lastname like '%".$search."%'
				   OR com_comment like '%".$search."%'
				   OR com_date_create like '%".$search."%')";
		
		if ($order_by == '')
			$order_by = ' ORDER BY com_date_create DESC ';
			
		$where_user = '';
		if ( $_SESSION['use_type'] == 'Adviser' )
			$where_user = "  AND use_type='Adviser' ";	
					
		$sql = "
		SELECT t1.com_code, t1.case_code, t1.com_comment, t1.com_date_create, t1.use_code, t1.cli_code, t1.case_number, t1.cli_name, 
		t1.cli_lastname, u.use_name, u.use_lastname FROM
		
		(SELECT t.com_code, t.case_code, t.com_comment, t.com_date_create, t.use_code, t.cli_code, t.case_number, c.cli_name, 
		c.cli_lastname FROM
		(SELECT c.com_code, c.case_code, c.com_comment, c.com_date_create, c.use_code, mc.cli_code, mc.case_number FROM comment c
		LEFT JOIN my_case mc ON c.case_code=mc.case_code) t		
		LEFT JOIN client c ON t.cli_code = c.cli_code WHERE c.cli_status <> 'Deleted') t1
		LEFT JOIN user u ON t1.use_code=u.use_code".$where." ".$where_user."  ".$order_by."";
		
		if ( !($inicio == 0 && $cantidad == 0 ) )
			$sql .= ' LIMIT '.$inicio.', '.$cantidad.';';
		
		$consult  = mysql_query($sql);
		$data;
		$i = 0;
		while($row = mysql_fetch_array($consult)){
			$data[$i]->com_code= $row['com_code'];
			$data[$i]->cli_code= $row['cli_code'];
			$data[$i]->case_code = $row['case_code'];
			$data[$i]->com_comment= $row['com_comment'];
			$data[$i]->com_date_create= $row['com_date_create'];
			$data[$i]->use_code= $row['use_code'];
			$data[$i]->cli_name= $row['cli_name'];
			$data[$i]->cli_lastname= $row['cli_lastname'];
			$data[$i]->case_number= $row['case_number'];
			$data[$i]->use_name= $row['use_name'];
			$data[$i]->use_lastname= $row['use_lastname'];

			$i++;				
		}
		return $data;
	}

	/**
	*	
	*	@autor: JJR
	*	@date: 01/28/2012
	*	Funcion encargada de contar la cantidad de comentarios.
	*	@return:
	*/

	function countComment($search=''){		
			
		$where = '';		
		if($search != '')
		$where = "  WHERE (	           
				    cli_name like '%".$search."%'
				   OR cli_lastname like '%".$search."%'
				   OR case_number like '%".$search."%'
				   OR use_name like '%".$search."%'
				   OR use_lastname like '%".$search."%'
				   OR com_comment like '%".$search."%'
				   OR com_date_create like '%".$search."%')";
		
		if ($order_by == '')
			$order_by = ' ORDER BY com_date_create DESC ';
			
		$where_user = '';
		if ( $_SESSION['use_type'] == 'Adviser' )
			$where_user = "  AND use_type='Adviser' ";	
		
		$sql = "SELECT COUNT(*) as cant FROM
		(SELECT t.com_code, t.case_code, t.com_comment, t.com_date_create, t.use_code, t.cli_code, t.case_number, c.cli_name, 
		c.cli_lastname FROM
		(SELECT c.com_code, c.case_code, c.com_comment, c.com_date_create, c.use_code, mc.cli_code, mc.case_number FROM comment c
		JOIN my_case mc ON c.case_code=mc.case_code) t		
		JOIN client c ON t.cli_code = c.cli_code WHERE c.cli_status <> 'Deleted') t1
		JOIN user u ON t1.use_code=u.use_code".$where." ".$where_user." ".$order_by."";
		
		if ( !($inicio == 0 && $cantidad == 0 ) )
			$sql .= ' LIMIT '.$inicio.', '.$cantidad.';';
		$consulta = mysql_query($sql);
		$row = mysql_fetch_array($consulta);
			
		return $row['cant'];
	}
	
	/**
	*	
	*	@autor: JJR
	*	@date: 01/27/2012
	*	Funcion encargada de obtener los datos de un cliente.
	*	@return:
	*/	
	
	function getClientData($cli_code)
	{
		 $sql = "SELECT c.cli_code, c.cli_name, c.cli_lastname
		FROM client c WHERE c.cli_status<> 'Deleted' AND c.cli_code = ".$cli_code;
		$consult = mysql_query($sql);
		$row = mysql_fetch_array($consult);
		
		$data->cli_code   				= $row['cli_code'] ;
		$data->cli_name   				= $row['cli_name'] ;
		$data->cli_lastname   			= $row['cli_lastname'] ;
			
		return $data;	
	}	
}
?>